package com.kitty.game.pet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.ExchangePet;
import com.kitty.game.config.NewPetSet;
import com.kitty.game.pet.bean.*;
import com.kitty.game.utils.Const;
import com.kitty.common.utils.StaticParam;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class PetDataPool {
    @Autowired
    Dao dao;
    /**兑换宠物的集合 key:name value:ExChange对象*/
    public static Map<String, ExchangePet> name2ExChangePet;

    /**所有宠物数据*/
    public static Map<String, PetObject> name2PetObject;

    /**宠物技能分类列表*/
    public static Map<Byte, List<String>> petSkillMap;

    /**所有宠物神兽数据*/
    public static Map<String, PetObject> epic_pets = new HashMap<>();
    /**所有变异宠物数据*/
    public static Map<String, PetObject> variation_pets = new HashMap<>();
    /**所有精怪宠物数据*/
    public static Map<String, PetObject> jingguai_pets = new HashMap<>();

    /**所有地府宠物数据*/
    public static Map<String, PetObject> ghost_pets = new HashMap<>();

    public static List<String> dunwuSkill = new ArrayList<>();



    /**加载所有宠物数据*/
    public static void loadCommonSet(Map<String, String> commonSetMap) {
        name2PetObject = new HashMap<>() ;
        epic_pets = new HashMap<>() ;
        variation_pets = new HashMap<>() ;
        jingguai_pets = new HashMap<>() ;
        ghost_pets = new HashMap<>() ;
        petSkillMap  = new HashMap<>() ;
        Dao dao = SpringUtils.getBean(Dao.class);
        List<NewPetSet> list = dao.query(NewPetSet.class, Cnd.NEW());
        for (NewPetSet newPetSet : list) {
            PetObject petObject = null;
            String name = newPetSet.getName();
            int type = newPetSet.getBigPetType();
            if(type == PetType.NORMAL){
                petObject = new NormalPetObject();
            }else if(type == PetType.ELITE){
                petObject = new VariationPetObject();
                variation_pets.put(name,petObject);
            }else if(type == PetType.EPIC){
                petObject = new EpicPetObject();
                epic_pets.put(name,petObject);
            }else if(type == PetType.OTHER){
                petObject = new OtherPetObject();
            }else if(type == PetType.JINGGUAI){
                petObject = new JingguaiPetObject();
                jingguai_pets.put(name,petObject);
            }else if(type == PetType.JINIAN){
                petObject = new CommemoratePetObject();
            }else if(type == PetType.ghost){
                petObject = new GhostPetObject();
                ghost_pets.put(name,petObject);
            }else{
                petObject = new NormalPetObject();
            }
            petObject.setIcon(newPetSet.getIcon());
            petObject.setLevel_req(newPetSet.getLevelReq());
            petObject.setLife(newPetSet.getLife());
            petObject.setMag_attack(newPetSet.getMagAttack());
            petObject.setMana(newPetSet.getMana());
            petObject.setName(name);
            petObject.setPhy_attack(newPetSet.getPhyAttack());
            petObject.setPolar(StaticParam.getPolarByNumber(newPetSet.getPolar()));
            petObject.setSkills(newPetSet.getSkills());
            petObject.setSpeed(newPetSet.getSpeed());
            petObject.setZoon(newPetSet.getZoon());
            petObject.setCapacity_level(newPetSet.getMoveSpeed());
            petObject.setTargetNum(newPetSet.getTargetNum());
            name2PetObject.put(newPetSet.getName(),petObject);
        }
        Map<String, NormalPetObject> normalPets = JSON.parseObject(commonSetMap.get("normal_pets"),new TypeReference<Map<String, NormalPetObject>>() {});
        for (Map.Entry<String,NormalPetObject> entry:normalPets.entrySet()){
            entry.getValue().setName(entry.getKey());
            name2PetObject.put(entry.getKey(),entry.getValue());
        }
        List innateSkills = JSON.parseObject(commonSetMap.get("pet_innate_skills"), List.class);
        List developSkills = JSON.parseObject(commonSetMap.get("pet_develop_skills"), List.class);
        Map<Byte, List<String>> petSkillMap = new HashMap<>();
        petSkillMap.put(Const.PET_SKILL_INNATE, innateSkills);
        petSkillMap.put(Const.PET_SKILL_DEVELOP, developSkills);

        PetDataPool.petSkillMap = petSkillMap;
        dunwuSkill.clear();
        dunwuSkill.add("如意圈");
        dunwuSkill.add("神圣之光");
        dunwuSkill.add("游说之舌");
        dunwuSkill.add("五色光环");
        dunwuSkill.add("法力护盾");
        dunwuSkill.add("移花接木");
        dunwuSkill.add("舍身取义");
        dunwuSkill.add("舍命一击");
        dunwuSkill.add("乾坤罩");
        dunwuSkill.add("死亡缠绵");
        dunwuSkill.add("神龙罩");
        dunwuSkill.add("漫天血舞");
        dunwuSkill.add("翻转乾坤");

    }


    /**宠物名取宠物配置对象*/
    public static PetObject getPetObject(String name){
        return name2PetObject.get(name);
    }
    /**宠物名取地府宠物对象*/
    public static GhostPetObject getGhostPetObject(String name){
        return (GhostPetObject)ghost_pets.get(name);
    }

    /**取所有变异*/
    public static List<PetObject> getElitePets(){
        List<PetObject> list =new ArrayList<>();
        name2PetObject.values().forEach(petObject -> {
            if(petObject instanceof VariationPetObject){
                list.add(petObject);
            }
        });
        return list;
    }

    public static List<String> getRandom(){
        return dunwuSkill;
    }

    private static boolean isSkillType(byte skillType, String skillName) {
        List<String> list = petSkillMap.get(skillType);
        return list.contains(skillName);
    }

    /**是否天生技能*/
    public static boolean isInnateSkill(String skillName) {
        return isSkillType(Const.PET_SKILL_INNATE, skillName);
    }

    /**是否研发技能*/
    public static boolean isDevelopSkill(String skillName) {
        return isSkillType(Const.PET_SKILL_DEVELOP, skillName);
    }
}
